Ext.BLANK_IMAGE_URL='../resources/images/default/s.gif';
//在ie中默认的宽度不够
Ext.apply(Ext.MessageBox,{
    alert:function(title, msg, fn, scope){
        this.show({
            title : title,
            msg : msg,
            buttons: this.OK,
            minWidth:200,
            fn: fn,
            scope : scope
        });
        return this;
    }
});


Ext.apply(Ext.form.VTypes,{
    password:function(val,field){
        if(field.initialPassField){
            var pwd=Ext.getCmp(field.initialPassField);
            return (val==pwd.getValue());
        }
        return true;
    },
    passwordText:'两次输入的密码不一致'
})
Ext.onReady(function(){
    Ext.lib.Ajax.defaultPostHeader += '; charset=utf-8';
    
    var saveURL='../hf/servlet/FileUploadServlet';
    
    Ext.QuickTips.init();
    
    Ext.form.Field.prototype.msgTarget = 'side';
    //第一步的字段布局
    var form1=new Ext.Panel({
        id:'card-0',
        layout:'form',
        frame:true,
        defaults:{width:230},
        defaultType:'textfield',
        
        items:[{
            fieldLabel:'First Name',
            name:'firstName',
            allowBlank:false,
            value:'黄峰'
        },{
            fieldLabel:'Last Name',
            allowBlank:false,
            name:'lastName',
            value:'Slocum'
        },{
            fieldLabel:'file',
            inputType: 'file',
            id:'file',
            tabIndex:3,
            name:'file1'
        },{
            fieldLabel:'password',
            inputType: 'password',
            id:'password',
            allowBlank:false,
            value:'a',
            name:'passworda'
        },{
            fieldLabel:'confirmPassword',
            inputType: 'password',
            initialPassField:'password',
            allowBlank:false,
            vtype:'password',
            value:'a',
            name:'confirmPassword'
        }]
    });
    //第二步的字段布局
    var form2=new Ext.Panel({
        id:'card-1',
        frame:true,
        layout:'form',
        defaults:{width:230},
        defaultType:'textfield',
                
        items:[{
            fieldLabel:'HomePhone',
            allowBlank:false,
            name:'homePhone',
            value:'(888) 555-1212'
        },{
            fieldLabel:'Mobile',
            allowBlank:false,
            name:'mobile'
        },{
            fieldLabel:'Fax',
            allowBlank:false,
            name:'fax'
        }]
    });
    //第三步的字段布局
    var form3=new Ext.Panel({
        id:'card-2',
        frame:true,
        layout:'form',
        labelWidth: 75, // label settings here cascade unless overridden
        width: 350,
        defaults: {width: 230},
        defaultType: 'textfield',

        items: [{
                fieldLabel: 'First Name',
                name: 'first',
                allowBlank:false
            },{
                fieldLabel: 'Last Name',
                name: 'last'
            },{
                fieldLabel: 'Company',
                name: 'company'
            }, {
                fieldLabel: 'Email',
                name: 'email',
                allowBlank:false,
                vtype:'email'
            }, new Ext.form.TimeField({
                fieldLabel: 'Time',
                name: 'time',
                minValue: '8:00am',
                maxValue: '6:00pm'
            })
        ]
    });
    
    var runner=new Ext.util.TaskRunner();       
    var task={
        run:function(){
            Ext.Ajax.request({                  //调用Ajax发送请求到后台
                url:'../hf/servlet/FileUploadProgressServlet',                  //删除grid数据的url.
                success:function(transport){                //处理成功后返回的函数
                    var oXmlDom=transport.responseText;             
                    Ext.log(oXmlDom);
                    if(oXmlDom==1){                     
                        runner.stop(task);
                    }                   
                },
                failure:function(){             //处理后台删除失败的函数
                	runner.stop(task);
                    Ext.MessageBox.alert('消息','上传失败,请联系管理员！');
                    
                }
            });
        },
        interval:100
    };

    
    //runner.start(task);
    /*
    var pbar=new Ext.ProgressBar({
        text:'Ready',
        id:'pbar',
        renderTo:'pbar'     
    });
    
    var btn2=Ext.get('btn2');
    btn2.on('click',function(){
        Runner.run(pbar,btn2,12,function(){
            pbar.reset();
            pbar.updateText('Done.');
        });
    })
    */
    var progressBar2=new Ext.ux.ProgressBar({
        text:'准备中...',
        relatingField:Ext.getCmp('file'),
        id:'pbar',
        el:'pbar',
        doneText:'上传完毕',
        url:'../hf/register.do?action=uploadProgress',
        resetProgressURL:'../hf/register.do?action=resetProgress'
    });
    Ext.ux.ProgressManager.addProgressBar(progressBar2);

    
    //实例化Ext.ux.WizardForm
    var wizardForm=new Ext.ux.WizardForm({
        title:'注册',
        url:saveURL,
        fileUpload:true,
        iconCls:'title-form-image',
        height:Ext.get('temp').getHeight(),
        forms:[form1]
    });
        
})

var Runner=function(){
    var f=function(v,pbar,btn,cb){
        return function(){
            if(v==1){
                //启动按钮
                btn.dom.disabled=false;
                cb();
            }else{
                pbar.updateProgress(v,'Loading item'+v+'%');
            }
        };
    }
    
    return{
        run:function(pbar,btn,count,cb){
            btn.dom.disabled=true;
            var runner=new Ext.util.TaskRunner();       
            var task={
                run:function(){
                    Ext.Ajax.request({                  //调用Ajax发送请求到后台
                        url:'../hf/servlet/FileUploadProgressServlet',                  //删除grid数据的url.
                        success:function(transport){                //处理成功后返回的函数
                            //debugger;
                            var oXmlDom=transport.responseText;                         

                            f(oXmlDom,pbar,btn,cb)();
                        },
                        failure:function(){             //处理后台删除失败的函数
                            Ext.MessageBox.alert('消息','上传失败,请联系管理员!');
                        }
                    });
                },
                interval:100
            };
            
            runner.start(task);

        }
    };
}();